﻿namespace eProject.ViewModel.Commands
{
    public class ChangeTaskCommand : ICommand
    {
        private readonly CommonManager _manager;

        private bool _isCanceled;
        private bool _isExecuted;
        private readonly string _oldName;
        private readonly string _oldDescription;
        private readonly long _oldDuration;
        private readonly string _newName;
        private readonly string _newDescription;
        private readonly long _newDuration;

        public ChangeTaskCommand(CommonManager manager, TaskViewModel task, string newName, string newDescription, long newDuration)
        {
            _manager = manager;
            _oldName = task.Name;
            _oldDescription = task.Description;
            _oldDuration = task.Duration;
            _newName = newName;
            _newDescription = newDescription;
            _newDuration = newDuration;
        }       

        public void Execute()
        {
            if (_isExecuted) return;
            var task = _manager.GetTask(_oldName);
            task.Name =  _newName;
            task.Description = _newDescription;
            task.Duration = _newDuration;
            _isExecuted = true;
            _isCanceled = false;
        }

        public void Unexecute()
        {
            if (_isCanceled) return;
            
            var task = _manager.GetTask(_newName);            
            task.Name = _oldName;
            task.Description = _oldDescription;
            task.Duration = _oldDuration;
            _isCanceled = true;
            _isExecuted = false;
        }
    }
}
